package com.imooc.controller;

import com.imooc.dto.OrderDTO;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.SellException;
import com.imooc.service.OrderMasterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author liuhuan
 * @date 2018/7/21 13:38
 * @description
 */
@Slf4j
@Controller
@RequestMapping("/sell/order")
public class SellOrderController {

    @Autowired
    private OrderMasterService orderMasterService;

    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "size",defaultValue = "10") Integer size,
                             Map<String,Object> map){
        Pageable request=new PageRequest(page-1,size);
        Page<OrderDTO> orderDTOPage=orderMasterService.findAll(request);
        map.put("orderDTOPage",orderDTOPage);
        map.put("currentPage",page);
        map.put("size",size);
        return new ModelAndView("order/list",map);

    }

    /**
     * 取消订单
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("cancel")
    public ModelAndView cancel(@RequestParam(value = "orderId") String orderId
            ,Map<String,Object> map){
        try {
            OrderDTO orderDTO=orderMasterService.findOne(orderId);
            orderMasterService.cancel(orderDTO);
        }catch (SellException e){
            log.error("【卖家端取消订单】查询不到订单");
            map.put("msg", e.getMessage());
            map.put("url","sell/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("msg", ResultEnum.ORDER_CANCEL_SUCCESS.getMsg());
        map.put("url","sell/order/list");
        return new ModelAndView("common/success",map);

    }


    /**
     * 订单详情
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("detail")
    public ModelAndView detail(@RequestParam(value = "orderId") String orderId
            ,Map<String,Object> map){
        OrderDTO orderDTO=null;
        try {
            orderDTO=orderMasterService.findOne(orderId);
        }catch (SellException e){
            log.error("【卖家端查询订单详情】查询不到订单详情");
            map.put("msg", e.getMessage());
            map.put("url","sell/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("orderDTO",orderDTO);
        return new ModelAndView("order/detail",map);

    }

    /**
     * 完结订单
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("finish")
    public ModelAndView finish(@RequestParam(value = "orderId") String orderId
            ,Map<String,Object> map){
        try {
            OrderDTO orderDTO=orderMasterService.findOne(orderId);
            orderMasterService.finish(orderDTO);
        }catch (SellException e){
            log.error("【卖家端完结订单】查询不到订单");
            map.put("msg", e.getMessage());
            map.put("url","sell/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("msg", ResultEnum.ORDER_CANCEL_SUCCESS.getMsg());
        map.put("url","sell/order/list");
        return new ModelAndView("common/success",map);

    }

}
